package com.findyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.findyou.mapper.HeadlineMapper;
import com.findyou.pojo.Headline;
import com.findyou.pojo.User;
import com.findyou.pojo.vo.PortalVo;
import com.findyou.service.HeadlineService;
import com.findyou.utils.JwtHelper;
import com.findyou.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline> implements HeadlineService {

    @Autowired
    private HeadlineMapper headlineMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result findNewsPage(PortalVo portalVo) {
        IPage<Map> page = new Page<>(portalVo.getPageNum(), portalVo.getPageSize());
        headlineMapper.findNewsPage(page, portalVo);

        List<Map> records = page.getRecords();
        Map data = new HashMap();
        Map pageData = new HashMap();
        pageData.put("pageData", records);


        pageData.put("pageNum", page.getCurrent());
        pageData.put("pageSize", page.getSize());
        pageData.put("totalPage", page.getPages());
        pageData.put("totalSize", page.getTotal());

        data.put("pageInfo", pageData);
        //System.out.println(data);
        return Result.ok(data);
    }

    @Override
    public Result showHeadlineDetail(int hid) {
        Map map = new HashMap();
        Map map1 = headlineMapper.showHeadlineDetail(hid);
       // System.out.println(map1);
        Headline headline = new Headline();

        headline.setHid(hid);
        headline.setPageViews((int)map1.get("pageViews") + 1);
        headline.setVersion((Integer) map1.get("version"));

      //  System.out.println(headline);
        headlineMapper.updateByMyId(headline.getPageViews() , headline.getHid());


        map.put("headline", map1);
       // System.out.println(map);
        return Result.ok(map);
    }

    @Override
    public Result publish(Headline headline, String token) {
        int hid = Math.toIntExact(jwtHelper.getUserId(token));
        headline.setPublisher(hid);
        headline.setUpdateTime(new Date());
        headline.setCreateTime(new Date());
        headline.setPageViews(0);
        headlineMapper.publish(headline);
        return Result.ok(null);
    }

    @Override
    public Result findHeadlineByHid(int hid) {
        Headline headline = headlineMapper.findHeadlineByHid(hid);
        Map data = new HashMap();
        data.put("headline", headline);
        return  Result.ok(data);
    }

    @Override
    public Result updateByMySelf(Headline headline) {

        Integer version = headlineMapper.selectByMyId(headline.getHid()).getVersion();
        headline.setVersion(version);
        headline.setUpdateTime(new Date());
        headlineMapper.MyupdateById(headline);
        return Result.ok(null);
    }

    @Override
    public void removeByHid(int hid) {
        headlineMapper.Mydelete(hid);
    }


}